package ioc

import (
	"gitee.com/xiao_hange/go-admin-log/log/ioc/config"
	"gitee.com/xiao_hange/go-admin-log/log/repository/dao"
	"github.com/olivere/elastic/v7"
	"github.com/spf13/viper"
)

// InitESClient 读取配置文件，进行初始化ES客户端
func InitESClient(es *config.EsConfig) *elastic.Client {
	type config struct {
		Addr []string `yaml:"addr"`
	}
	var cfg config
	err := viper.UnmarshalKey("es", &cfg)
	client, err := elastic.NewClient(
		elastic.SetURL(cfg.Addr...),
		elastic.SetSniff(false),
	)
	if err != nil {
		panic(err)
	}
	err = dao.InitES(client)
	if err != nil {
		panic(err)
	}
	return client
}
